package com.ruoyi.xysl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.xysl.domain.dto.XyslUserDto;
import com.ruoyi.xysl.service.ISysXyslRoleService;
import com.xysl.api.domain.XyslRole;
import com.xysl.api.domain.XyslUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 角色Controller
 *
 * @author xysl
 * @date 2025-01-10
 */
@RestController
@RequestMapping("/xyslRole")
public class SysXyslRoleController extends BaseController
{
    @Autowired
    private ISysXyslRoleService xyslRoleService;

    /**
     * 查询角色列表
     */
    @RequiresPermissions("xysl:role:list")
    @GetMapping("/list")
    public TableDataInfo list(XyslRole xyslRole)
    {
        startPage();
        List<XyslRole> list = xyslRoleService.selectXyslRoleList(xyslRole);
        return getDataTable(list);
    }

    /**
     * 查询选择上级角色列表
     */
    @RequiresPermissions("xysl:role:list")
    @GetMapping("/roleSelection/{roleId}")
    public AjaxResult roleSelection(@PathVariable("roleId") Long roleId)
    {
        startPage();
        List<XyslRole> list = null;
        if (null != roleId && roleId.toString().equals("-1")) {
            list = xyslRoleService.selectXyslRoleList(new XyslRole());
        }else {
            list = xyslRoleService.selectParentRoleList(roleId);
        }
        return success(list);
    }


    /**
     * 导出角色列表
     */
    @RequiresPermissions("xysl:role:export")
    @Log(title = "角色", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XyslRole xyslRole)
    {
        List<XyslRole> list = xyslRoleService.selectXyslRoleList(xyslRole);
        ExcelUtil<XyslRole> util = new ExcelUtil<XyslRole>(XyslRole.class);
        util.exportExcel(response, list, "角色数据");
    }

    /**
     * 获取角色详细信息
     */
    @RequiresPermissions("xysl:role:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(xyslRoleService.selectXyslRoleById(id));
    }

    /**
     * 新增角色
     */
    @RequiresPermissions("xysl:role:add")
    @Log(title = "角色", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XyslRole xyslRole)
    {
        return toAjax(xyslRoleService.insertXyslRole(xyslRole));
    }

    /**
     * 修改角色
     */
    @RequiresPermissions("xysl:role:edit")
    @Log(title = "角色", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XyslRole role)
    {
        if (!xyslRoleService.checkRoleNameUnique(role))
        {
            return error("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        }
        else if (!xyslRoleService.checkRoleCodeUnique(role))
        {
            return error("修改角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
        return toAjax(xyslRoleService.updateXyslRole(role));
    }

    /**
     * 删除角色
     */
    @RequiresPermissions("xysl:role:remove")
    @Log(title = "角色", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xyslRoleService.deleteXyslRoleByIds(ids));
    }


    /**
     * 获取角色下拉树列表
     */
    @GetMapping("/roleTreeselect")
    public AjaxResult roleTreeselect(XyslRole xyslRole)
    {
        List<XyslRole> list = xyslRoleService.selectXyslRoleList(xyslRole);
        return success(xyslRoleService.buildRoleTreeSelect(list));
    }

    /**
     * 批量选择用户授权
     */
    @RequiresPermissions("xysl:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/selectAll")
    public AjaxResult selectAuthUserAll(Long roleId, Long[] userIds)
    {
        return toAjax(xyslRoleService.insertAuthUsers(roleId, userIds));
    }

    /**
     * 批量选择用户授权
     */
    @RequiresPermissions("xysl:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @GetMapping("/authUser/allocatedList")
    public TableDataInfo allocatedList(XyslUserDto user)
    {
        startPage();
        List<XyslUser> list = xyslRoleService.selectAllocatedList(user);
        return getDataTable(list);
    }


    /**
     * 查询未分配用户角色列表
     */
    @RequiresPermissions("xysl:role:list")
    @GetMapping("/authUser/unallocatedList")
    public TableDataInfo unallocatedList(XyslUserDto user)
    {
        startPage();
        List<XyslUser> list = xyslRoleService.selectUnallocatedList(user);
        return getDataTable(list);
    }


    /**
     * 取消用户角色
     */
    @RequiresPermissions("xysl:role:edit")
    @Log(title = "xysl取消用户角色", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancel")
    public AjaxResult cancelAuthUser(@RequestBody SysUserRole userRole)
    {
        return toAjax(1);
    }

    /**
     * 批量 取消用户角色
     */
    @RequiresPermissions("xysl:role:edit")
    @Log(title = "xysl批量_取消用户角色", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancelAll")
    public AjaxResult cancelAuthUserBath(Long roleId, Long[] userIds)
    {
        return toAjax(xyslRoleService.cancelAuthUserBath(roleId, userIds));
    }
}
